//window resize events
$(window).resize(function() {
  //get the window size
  var wsize = $(window).width();
  if (wsize > 980) {
    $('.shortcuts.hided').removeClass('hided').attr("style", "");
    $('.sidenav.hided').removeClass('hided').attr("style", "");
  }

  var size = "Window size is:" + $(window).width();
  //console.log(size);
});

$(window).load(function() {
  var wheight = $(window).height();
  $('#sidebar.scrolled').css('height', wheight - 63 + 'px');
});

// document ready function
$(document).ready(function() {

  //prevent font flickering in some browsers
  (function() {
    //if firefox 3.5+, hide content till load (or 3 seconds) to prevent FOUT
    var d = document, e = d.documentElement, s = d.createElement('style');
    if (e.style.MozTransform === '') { // gecko 1.9.1 inference
      s.textContent = 'body{visibility:hidden}';
      e.firstChild.appendChild(s);
      function f() {
        s.parentNode && s.parentNode.removeChild(s);
      }
      addEventListener('load', f, false);
      setTimeout(f, 3000);
    }
  })();

  //Disable certain links
  $('a[href^=#]').click(function(e) {
    e.preventDefault()
  })

  $('.search-btn').addClass('nostyle');//tell uniform to not style this element


  //------------- Navigation -------------//

  mainNav = $('.mainnav>ul>li');
  mainNav.find('ul').siblings().addClass('hasUl').append('<span class="hasDrop icon16 icomoon-icon-arrow-down-2"></span>');
  mainNavLink = mainNav.find('a').not('.sub a');
  mainNavLinkAll = mainNav.find('a');
  mainNavSubLink = mainNav.find('.sub a').not('.sub li .sub a');
  mainNavCurrent = mainNav.find('a.current');

  //remove current class if have
  mainNavCurrent.removeClass('current');
  //set the seleceted menu element
  if ($.cookie("newCurrentMenu")) {
    mainNavLinkAll.each(function(index) {
      if ($(this).attr('href') == $.cookie("newCurrentMenu")) {
        //set new current class
        $(this).addClass('current');

        ulElem = $(this).closest('ul');
        if (ulElem.hasClass('sub')) {
          //its a part of sub menu need to expand this menu
          aElem = ulElem.prev('a.hasUl').addClass('drop');
          ulElem.addClass('expand');
        }
        //create new cookie
        $.cookie("currentPage", $(this).attr('href'));
      }
    });
  }

  //hover magic add blue color to icons when hover - remove or change the class if not you like.
  mainNavLinkAll.hover(
          function() {
            $(this).find('span.icon16').addClass('blue');
          },
          function() {
            $(this).find('span.icon16').removeClass('blue');
          }
  );

  //click magic
  mainNavLink.click(function(event) {
    $this = $(this);

    if ($this.hasClass('hasUl')) {
      event.preventDefault();
      if ($this.hasClass('drop')) {
        $(this).siblings('ul.sub').slideUp(500, 'jswing').siblings().removeClass('drop');
      } else {
        $(this).siblings('ul.sub').slideDown(500, 'jswing').siblings().addClass('drop');
      }
    } else {
      //has no ul so store a cookie for change class.
      $.cookie("newCurrentMenu", $this.attr('href'), {expires: null});
    }
  });
  mainNavSubLink.click(function(event) {
    $this = $(this);

    if ($this.hasClass('hasUl')) {
      event.preventDefault();
      if ($this.hasClass('drop')) {
        $(this).siblings('ul.sub').slideUp(500).siblings().removeClass('drop');
      } else {
        $(this).siblings('ul.sub').slideDown(250).siblings().addClass('drop');
      }
    } else {
      //has no ul so store a cookie for change class.
      $.cookie("newCurrentMenu", $this.attr('href'), {expires: null});
    }
  });

  //responsive buttons
  $('.resBtn>a').click(function(event) {
    $this = $(this);
    if ($this.hasClass('drop')) {
      $('#sidebar>.shortcuts').slideUp(500).addClass('hided');
      $('#sidebar>.sidenav').slideUp(500).addClass('hided');
      $('#sidebar-right>.shortcuts').slideUp(500).addClass('hided');
      $('#sidebar-right>.sidenav').slideUp(500).addClass('hided');
      $this.removeClass('drop');
    } else {
      if ($('#sidebar').length) {
        $('#sidebar').css('display', 'block');
        if ($('#sidebar-right').length) {
          $('#sidebar-right').css({'display': 'block', 'margin-top': '0'});
        }
      }
      if ($('#sidebar-right').length) {
        $('#sidebar-right').css('display', 'block');
      }
      $('#sidebar>.shortcuts').slideDown(250);
      $('#sidebar>.sidenav').slideDown(250);
      $('#sidebar-right>.shortcuts').slideDown(250);
      $('#sidebar-right>.sidenav').slideDown(250);
      $this.addClass('drop');
    }
  });
  $('.resBtnSearch>a').click(function(event) {
    $this = $(this);
    if ($this.hasClass('drop')) {
      $('.search').slideUp(500);
      $this.removeClass('drop');
    } else {
      $('.search').slideDown(250);
      $this.addClass('drop');
    }
  });

  //Hide and show sidebar btn

  $(function() {
    //var pages = ['grid.html','charts.html'];
    var pages = [];

    for (var i = 0, j = pages.length; i < j; i++) {

      if ($.cookie("currentPage") == pages[i]) {
        var cBtn = $('.collapseBtn.leftbar');
        cBtn.children('a').attr('title', 'Show Left Sidebar');
        cBtn.addClass('shadow hide');
        cBtn.css({'top': '20px', 'left': '200px'});
        $('#sidebarbg').css('margin-left', '-299' + 'px');
        $('#sidebar').css('margin-left', '-299' + 'px');
        if ($('#content').length) {
          $('#content').css('margin-left', '0');
        }
        if ($('#content-two').length) {
          $('#content-two').css('margin-left', '0');
        }
      }

    }

  });

  $('.collapseBtn').bind('click', function() {
    $this = $(this);

    //left sidbar clicked
    if ($this.hasClass('leftbar')) {

      if ($(this).hasClass('hide')) {
        //show sidebar
        $('#sidebarbg').css('margin-left', '0');
        $('#content').css('margin-left', '213' + 'px');
        $('#content-two').css('margin-left', '213' + 'px');
        $('#sidebar').css({'left': '0', 'margin-left': '0'});

        $this.removeClass('hide');
        $('.collapseBtn.leftbar').css('top', '120' + 'px').css('left', '170' + 'px').removeClass('shadow');
        $this.children('a').attr('title', 'Hide Left Sidebar');

      } else {
        //hide sidebar
        $('#sidebarbg').css('margin-left', '-299' + 'px');
        $('#sidebar').css('margin-left', '-299' + 'px');
        $('.collapseBtn.leftbar').animate({//use .hide() if you experience heavy animation :)
          left: '200',
          top: '20'
        }, 500, 'easeInExpo', function() {
          // Animation complete.

        }).addClass('shadow');
        //expand content
        $this.addClass('hide');
        $this.children('a').attr('title', 'Show Left Sidebar');
        if ($('#content').length) {
          $('#content').css('margin-left', '0');
        }
        if ($('#content-two').length) {
          $('#content-two').css('margin-left', '0');
        }

      }

    }

    //right sidebar clicked
    if ($this.hasClass('rightbar')) {

      if ($(this).hasClass('hide')) {
        //show sidebar
        $('#sidebarbg-right').css('margin-right', '0');
        $('#sidebar-right').css({'right': '0', 'margin-right': '0'});
        if ($('#content').length) {
          $('#content').css('margin-left', '213' + 'px');
        }
        if ($('#content-one').length) {
          $('#content-one').css('margin-right', '212' + 'px');
        }
        if ($('#content-two').length) {
          $('#content-two').css({'margin-right': '212' + 'px'});
        }
        /*if($('#sidebar').length) {
         $('#sidebar').css({'left' : '0', 'margin-left' : '0'});
         }*/
        $this.removeClass('hide');
        $('.collapseBtn.rightbar').css('top', '120' + 'px').css('right', '18' + 'px').removeClass('shadow');
        $this.children('a').attr('title', 'Hide Right Sidebar');

      } else {
        //hide sidebar
        $('#sidebarbg-right').css('margin-right', '-299' + 'px');
        $('#sidebar-right').css('margin-right', '-299' + 'px');
        if ($('#content').length) {
          $('#content').css('margin-right', '0');
        }
        if ($('#content-one').length) {
          $('#content-one').css({'margin-left': '0', 'margin-right': '0'});
        }
        if ($('#content-two').length) {
          $('#content-two').css({'margin-right': '0'});
        }
        $('.collapseBtn.rightbar').animate({//use .hide() if you experience heavy animation :)
          right: '10',
          top: '78'
        }, 500, 'easeInExpo', function() {
          // Animation complete.

        }).addClass('shadow');
        //expand content
        $this.addClass('hide');
        $this.children('a').attr('title', 'Show Right Sidebar')
      }

    }
  });


  //------------- widget box magic -------------//

  var widget = $('div.box');
  var widgetOpen = $('div.box').not('div.box.closed');
  var widgetClose = $('div.box.closed');
  //close all widgets with class "closed"
  widgetClose.find('div.content').hide();
  widgetClose.find('.title>.minimize').removeClass('minimize').addClass('maximize');

  widget.find('.title>a').click(function(event) {
    event.preventDefault();
    var $this = $(this);
    if ($this.hasClass('minimize')) {
      //minimize content
      $this.removeClass('minimize').addClass('maximize');
      $this.parent('div').addClass('min');
      cont = $this.parent('div').next('div.content')
      cont.slideUp(500, 'easeOutExpo'); //change effect if you want :)

    } else
    if ($this.hasClass('maximize')) {
      //minimize content
      $this.removeClass('maximize').addClass('minimize');
      $this.parent('div').removeClass('min');
      cont = $this.parent('div').next('div.content');
      cont.slideDown(500, 'easeInExpo'); //change effect if you want :)
    }

  })

  //show minimize and maximize icons
  widget.hover(function() {
    $(this).find('.title>a').show(50);
  }
  , function() {
    $(this).find('.title>a').hide();
  });

  //add shadow if hover box
  widget.hover(function() {
    $(this).addClass('hover');
  }
  , function() {
    $(this).removeClass('hover');
  });

  //------------- placeholder fallback  -------------//
  $('input[placeholder], textarea[placeholder]').placeholder();

  //------------- Search forms  submit handler  -------------//
  $('#search-form').submit(function() {
    return false;
  });

  //make custom redirect for search form in .heading
  $('#searchform').submit(function() {
    var sText = $('.top-search').val();
    var sAction = $(this).attr('action');
    var sUrl = sAction + '?q=' + sText;
    $(location).attr('href', sUrl);
    return false;
  });

  //------------- To top plugin  -------------//
  $().UItoTop({
    //containerID: 'toTop', // fading element id
    //containerHoverID: 'toTopHover', // fading element hover id
    //scrollSpeed: 1200,
    easingType: 'easeOutQuart'
  });

  //------------- Tooltips -------------//

  //top tooltip
  $('.tip').qtip({
    content: false,
    position: {
      my: 'bottom center',
      at: 'top center',
      viewport: $(window)
    },
    style: {
      classes: 'ui-tooltip-tipsy'
    }
  });

  //tooltip in right
  $('.tipR').qtip({
    content: false,
    position: {
      my: 'left center',
      at: 'right center',
      viewport: $(window)
    },
    style: {
      classes: 'ui-tooltip-tipsy'
    }
  });

  //tooltip in bottom
  $('.tipB').qtip({
    content: false,
    position: {
      my: 'top center',
      at: 'bottom center',
      viewport: $(window)
    },
    style: {
      classes: 'ui-tooltip-tipsy'
    }
  });

  //tooltip in left
  $('.tipL').qtip({
    content: false,
    position: {
      my: 'right center',
      at: 'left center',
      viewport: $(window)
    },
    style: {
      classes: 'ui-tooltip-tipsy'
    }
  });

  //------------- Search forms  submit handler  -------------//
  $('#search-form').submit(function() {
    return false;
  });

  //make custom redirect for search form in .heading
  $('#searchform').submit(function() {
    var sText = $('.top-search').val();
    var sAction = $(this).attr('action');
    var sUrl = sAction + '?q=' + sText;
    $(location).attr('href', sUrl);
    return false;
  });

  //------------- Uniform  -------------//
  //add class .nostyle if not want uniform to style field
  $("input, textarea, select").not('.nostyle').uniform();

  //remove loadstate class from body and show the page
  setTimeout('$("html").removeClass("loadstate")', 500);

  //image upload using ajax
  $('#image-upload-form').submit(function(e) {
    e.preventDefault();
    //show loading
    var formData = new FormData(this);
    formData.append('isAjax', true);

    $.ajax({
      type: 'POST',
      url: $(this).attr('action'),
      data: formData,
      cache: false,
      contentType: false,
      processData: false,
      success: function(data) {
        var responseJSON = $.parseJSON(data);
        var tr = '<tr>';
        tr += '<td>';
        tr +=   '<img src="'+responseJSON.url+'">';
        tr += '</td>';
        tr += '<td>';
        tr +=   '<input type="text" name="ordering['+responseJSON.image_id+']" />';
        tr += '</td>';
        tr += '<td>';
        tr +=   '<div class="controls center">'
        tr +=     '<a href="project/deleteImage/'+data.image_id+'" class="tip image-remove" oldtitle="Remove image" title="" aria-describedby="ui-tooltip-8"><span class="icon12 icomoon-icon-remove"></span></a>'
        tr += '</div>';
        tr += '</td>';
        tr += '</tr>';

        $('#table-project-images tbody').append(tr);
      },
      error: function(data) {
        console.log(data);
      }
    });
  });

  $('body').on('click', '.image-remove', function(e){
    e.preventDefault();

    $el = $(this);
    $.ajax({
      type: 'POST',
      url: $(this).attr('href'),
      data: {isAjax: true},
      cache: false,
      success: function(data) {
        var json = $.parseJSON(data);
        if(json.status){
          $el.closest('tr').remove();
        }
      },
      error: function(data) {
        console.log(data);
      }
    });
  });

  $( 'textarea.editor' ).ckeditor();

  $('select[name="type"]').change(function(){
    var types = ['image', 'flash', 'html'];
    var val = $(this).val();

    for(var i = 0 ; i < types.length; i++){
      if(types[i] !== val){
        $('#type-'+types[i]).hide();
      }else{
        $('#type-'+types[i]).show();
      }
    }
  });

  $('select[name="type"]').trigger('change');

  if($('#datepicker').length) {
      $("#datepicker").datepicker({
          showOtherMonths:true
      });
  }
});